//
//  ConfirmTaskTableViewCell.m
//  leo
//
//  Created by 常艳 on 15/10/2.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "ConfirmTaskTableViewCell.h"
#import "Tooler.h"
#import "GrowManager.h"
#import "LeoMsg.h"
@implementation ConfirmTaskTableViewCell

@synthesize task;

- (void)awakeFromNib {
    // Initialization code
    UIImage *preCancleImg=[UIImage imageNamed:@"cancel_button_press@2x"];
    [self.unfinishBtn setBackgroundImage:preCancleImg forState:UIControlStateHighlighted];
    preCancleImg=NULL;
    UIImage *preConfirmImg=[UIImage imageNamed:@"confirm_button_press@2x"];
    [self.finishBtn setBackgroundImage:preConfirmImg forState:UIControlStateHighlighted];
    preConfirmImg=NULL;    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)initUI
{
//    self.contentView.layer.borderColor =  [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;
//
//    self.contentView.layer.borderWidth = 1.0;
    if(task==NULL)
        return;
    if(task.title!=NULL&&![task.title isEqualToString:@"(null)"])
        self.titleLable.text=task.title;
    if(task.desc!=NULL&&![task.desc isEqualToString:@"(null)"])
    {
        self.descLable.text=task.desc;     
    }
    if (task.createTime != NULL &&![task.createTime isEqualToString:@"(null)"]) {
        self.timeLabel.text = [Tooler getDate:task.createTime];
    }
    self.empLable.text=[NSString stringWithFormat:@"完成后获得%d点经验值",task.empirical];
    if(task.status==DONE)
    {

        self.statusImg.hidden = NO;
        self.statusImg.image=[UIImage imageNamed:@"task_already@2x.png"];
        self.finishBtn.enabled=NO;
        self.unfinishBtn.enabled=NO;
        self.finishBtn.hidden=YES;
        self.unfinishBtn.hidden=YES;
    }

    else if(task.status==UNDO)
    {

        self.statusImg.hidden = NO;
        self.statusImg.image=[UIImage imageNamed:@"task_unalready@2x.png"];
        self.finishBtn.enabled=NO;
        self.unfinishBtn.enabled=NO;
        self.finishBtn.hidden=YES;
        self.unfinishBtn.hidden=YES;

    }
    else if(task.status==EXPIRE||[Tooler istaskExpire:task])
    {
        self.statusImg.image=[UIImage imageNamed:@"@task_timeOut@2x.png"];
        self.finishBtn.enabled=NO;
        self.unfinishBtn.enabled=NO;
        self.finishBtn.hidden=YES;
        self.unfinishBtn.hidden=YES;
    }
    else if(task.status==DOING)
    {

        self.statusImg.hidden = YES;
        self.finishBtn.enabled=YES;
        self.unfinishBtn.enabled=YES;
        self.finishBtn.hidden=NO;
        self.unfinishBtn.hidden=NO;
    }
}

- (IBAction)unfinish:(id)sender {
    self.finishBtn.enabled=NO;
    self.unfinishBtn.enabled=NO;
    GrowManager *growManager=[[GrowManager alloc] init];
    if(growManager==NULL)
        return;
    [growManager confirmTask:task result:NO completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if(result==SUCESS)
        {
            task.status=UNDO;
            [self initUI];
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"操作失败"];
            self.finishBtn.enabled=YES;
            self.unfinishBtn.enabled=YES;
        }
    }];
}
- (IBAction)finish:(id)sender {
    self.finishBtn.enabled=NO;
    self.unfinishBtn.enabled=NO;
    GrowManager *growManager=[[GrowManager alloc] init];
    if(growManager==NULL)
        return;
    [growManager confirmTask:task result:YES completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
       if(result==SUCESS)
       {
           task.status=DONE;
           [self initUI];
       }
        else
        {
           [Tooler showHttpMsg:result description:description msg:@"操作失败"];
            self.finishBtn.enabled=YES;
            self.unfinishBtn.enabled=YES;
        }
    }];
}

@end
